package cn.cheqi.recovery.background.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 文件上传下载工具类
 */

public class SysFileUtils {

    //文件上传路径
    public static String UPLOAD_PATH = "C:/www/yihuanbao/img/";

    //文件的默认头像图片
    public static String Default_Img_Path = "default/default.jpg";

    //用户头像存储的文件夹
    public static String User_Save_Folder = "user/";

    //员工头像存储的文件夹
    public static String Emp_Save_Folder = "emp/";

    //小区图片存储的文件夹
    public static String Community_Save_Folder = "community/";


    static {
        //读取配置文件的值
        InputStream stream = SysFileUtils.class.getClassLoader()
                .getResourceAsStream("file.properties");
        Properties properties = new Properties();
        try {
            properties.load(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filePath = properties.getProperty("filePath");
        if (!StringUtils.isBlank(filePath)){
            UPLOAD_PATH=filePath;
        }
    }

    /**
     * 根据文件的老名字得到新的文件名
     * @param oldFileName
     * @return
     */
    public static String createNewFileName(String oldFileName) {
        //获取文件名的后缀
        String suffix = oldFileName.substring(oldFileName.lastIndexOf("."));
        return IdUtil.simpleUUID().toUpperCase()+suffix;
    }

    /**
     * 文件下载
     * @param path
     * @return
     */
    public static ResponseEntity<Object> createResponseEntity(String path) {
        //1.构造文件对象
        File file = new File(UPLOAD_PATH, path);
        if (file.exists()){
            byte[] bytes = null;
            try{
                bytes = FileUtil.readBytes(file);
            }catch (Exception e){
                e.printStackTrace();
            }
            //创建封装响应头信息的对象
            HttpHeaders header = new HttpHeaders();
            //封装响应内容类型(APPLICATION_OCTET_STREAM相应的内容不限定)
            header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            //设置下载的文件名称
            //header.setContentDispositionFormData("attachment","123.jpg");
            ResponseEntity<Object> entity =
                    new ResponseEntity<Object>(bytes,header, HttpStatus.CREATED);
            return entity;
        }
        return null;
    }

    /**
     * 去掉文件名的无用后缀
     * @param avatarUrl
     * @return
     */
    public static String reNameFile(String avatarUrl) {
        File file = new File(UPLOAD_PATH,avatarUrl);
        //去除多余后缀
        String fileName = avatarUrl.replace("_temp", "");
        //判断该文件在文件夹中是否存在
        if (file.exists()){
            file.renameTo(new File(UPLOAD_PATH,fileName));
        }
        return fileName;
    }


    /**
     * 去掉多文件的后缀
     * @param fileTemp
     */
    public static String moveAllTemp(String fileTemp){
        //通过逗号切割字符串
        String[] splitFile = fileTemp.split(",");
        StringBuilder sb = new StringBuilder();
        for (String file : splitFile) {
            File newFile = new File(UPLOAD_PATH,file);
            //去掉多余后缀后的文件名
            String afterTemp = file.replace("_temp", "");
            if (newFile.exists()){
                newFile.renameTo(new File(UPLOAD_PATH,afterTemp));
            }
            sb.append(afterTemp+",");
        }
        //去掉最后一个逗号
        sb.deleteCharAt(sb.lastIndexOf(","));
        return sb.toString();
    }

    /**
     * 删除旧的图片
     * @param oldPath
     */
    public static void removeOldFile(String oldPath) {
        File file = new File(UPLOAD_PATH,oldPath);
        if (file.exists()){
            file.delete();
        }
    }

    /**
     * 删除商家时连同商家的图片一并删除
     * @param communityPics
     */
    public static void deleteCommunityPics(String communityPics){
        //商家的多个图片路径
        String[] fileNames = communityPics.split(",");
        //遍历每个文件夹看是否存在
        for (String fileName : fileNames) {
            //以防删除默认图片
            if (!fileName.equals(Default_Img_Path)) {
                File file = new File(UPLOAD_PATH, fileName);
                if (file.exists()) {
                    file.delete();
                }
            }
        }
    }
}
